﻿var DEBUG = false;
var orderObj = null;
var currentIsFujin = null;
var orderList = null;


var addressId;
var addressName;

/**
 * 点击购物车，订单列表
 */
function gotoGwc() {
    if (!lat || !lng) {
        messageBox("尚未定位");
        return;
    }
    try {
        orderObj = {
            addressId: null,
            addressName: null,
            orderId: null,
            fruits: new Array(),
            shipPrice: null,
            fruitTotalPrice: null,
            limitFlag: null,
            shopName: null,
            shipStartTime: null,
            shipEndTime: null,
            shopId: null,
            shopAddress: null,
            faster: null,
            arriveBeginClock: null,
            arriveEndClock: null,
            orderClock: null,
            addressId: null,
            addressName: null
        };
        $('#page_8_1_listview_dd').remove();
        $('#page_8_1').html('<div data-role="header"style="border: none"><h1>我的订单</h1><a class="ui-btn ui-corner-all ui-btn-right "onClick="panel()"data-transition="slide"style=" border:none;margin-top:-1.5px;"><img src="menu.png"style="height:25px;width:25px"/></a><ul data-role="listview"id="page_8_1_listview_dd"></ul></div>');
        $('#page_8_1').trigger('create');
        var arr = JSON.parse(localStorage.getItem("GWC"));
        var arr2 = new Array();
        arr2[0] = new Array();
        arr2[1] = new Array();
        if (null == arr || arr.length <= 0) {
            messageBox("购物车无水果");
            changePage("#page_3_1");
            return
        }
        var len = arr.length;
        for (var i = 0; i < len; i++) {
            var o = arr[i];
            if (o.isFujin) {
                arr2[0].push(o)
            } else {
                arr2[1].push(o)
            }
        }
        var hasFlag = false;
        var arr3 = new Array();
        for (var i = 0; i < arr2[0].length; i++) {
            hasFlag = true;
            var o = arr2[0][i];
            var hasExist = false;
            for (var j = 0; j < arr3.length; j++) {
                if (arr3[j] == o.shopObj.shopId) {
                    hasExist = true;
                    break
                }
            }
            if (!hasExist) {
                arr3.push(o.shopObj.shopId);
                var t = o;
                JKjsonP({
                    ws: "customer/getShopById",
                    parameter: {
                        shopId: o.shopObj.shopId,
                        lat: lat,
                        lng: lng
                    },
                    sFunc: function(json) {
                        if (json.result == "success") {
                            var obj = json.data;
                            var distance31 = obj.distance;
                            var shippingArr = obj.shipping;
                            var len2 = shippingArr.length;
                            var distance312 = 0;
                            var txt = "";
                            if (distance31 <= 1000) {
                                distance312 = 1
                            } else if (distance31 > 1000 || distance31 <= 2000) {
                                distance312 = 2
                            } else if (distance31 > 2000 || distance31 <= 3000) {
                                distance312 = 3
                            } else if (distance31 > 3000 || distance31 <= 4000) {
                                distance312 = 4
                            } else if (distance31 > 4000 || distance31 <= 5000) {
                                distance312 = 5
                            }
                            for (var j = 0; j < len2; j++) {
                                if (distance312 == shippingArr[j].distance) {
                                    var shippingPrice31 = shippingArr[j].shippingPrice;
                                    var freeShippingPrice31 = shippingArr[j].freeShippingPrice;
                                    var startPrice31 = shippingArr[j].startPrice;
                                    if (shippingPrice31 == "0" && freeShippingPrice31 == "0") {
                                        txt = "满" + startPrice31 + "元起送"
                                    } else if (startPrice31 == "0") {
                                        txt = "满" + freeShippingPrice31 + "免" + shippingPrice31 + "元送货费"
                                    }
                                }
                            };
                            var arr = JSON.parse(localStorage.getItem("GWC"));
                            var fruitStr = '';
                            for (var i = 0; i < arr.length; i++) {
                                var obj2 = arr[i];
                                if (obj2.fruitObj.shop.id == obj.shopId && obj2.isFujin == true) {
                                    fruitStr = $.trim(fruitStr + ' ' + obj2.fruitObj.name)
                                }
                            }
                            $('#page_8_1_listview_dd').append('<li><a onclick="go83(' + obj.shopId + ', true)"href="javascript:void(0)"style="border-color:#FFFFFF"class="ui-btn ui-btn-icon-right ui-icon-carat-r"><div style="font-size:18px"><p>' + obj.shopName + '</p><p>附近水果摊的订单</p><p>营业时间：<span>' + obj.openTime + '-' + obj.closeTime + '</span></p><p style="white-space:pre-wrap">' + txt + '</p><p style="white-space:pre-wrap">' + fruitStr + '</p></div></a></li>');
                            var hasFlag2 = false;
                            arr3 = new Array();
                            if (arr2[1].length > 0) {
                                for (var i = 0; i < arr2[1].length; i++) {
                                    hasFlag2 = true;
                                    var o = arr2[1][i];
                                    var hasExist = false;
                                    for (var j = 0; j < arr3.length; j++) {
                                        if (arr3[j] == o.shopObj.shopId) {
                                            hasExist = true;
                                            break
                                        }
                                    }
                                    if (!hasExist) {
                                        arr3.push(o.shopObj.shopId);
                                        JKjsonP({
                                            ws: "customer/getShopById",
                                            parameter: {
                                                shopId: o.shopObj.shopId,
                                                lat: lat,
                                                lng: lng
                                            },
                                            sFunc: function(json) {
                                                if (json.result == "success") {
                                                    var obj = json.data;
                                                    var distance31 = obj.distance;
                                                    var shippingArr = obj.shipping;
                                                    var len2 = shippingArr.length;
                                                    var distance312 = 0;
                                                    var txt = "";
                                                    if (distance31 <= 1000) {
                                                        distance312 = 1
                                                    } else if (distance31 > 1000 || distance31 <= 2000) {
                                                        distance312 = 2
                                                    } else if (distance31 > 2000 || distance31 <= 3000) {
                                                        distance312 = 3
                                                    } else if (distance31 > 3000 || distance31 <= 4000) {
                                                        distance312 = 4
                                                    } else if (distance31 > 4000 || distance31 <= 5000) {
                                                        distance312 = 5
                                                    }
                                                    for (var j = 0; j < len2; j++) {
                                                        if (distance312 == shippingArr[j].distance) {
                                                            var shippingPrice31 = shippingArr[j].shippingPrice;
                                                            var freeShippingPrice31 = shippingArr[j].freeShippingPrice;
                                                            var startPrice31 = shippingArr[j].startPrice;
                                                            if (shippingPrice31 == "0" && freeShippingPrice31 == "0") {
                                                                txt = "满" + startPrice31 + "元起送"
                                                            } else if (startPrice31 == "0") {
                                                                txt = "满" + freeShippingPrice31 + "免" + shippingPrice31 + "元送货费"
                                                            }
                                                        }
                                                    };
                                                    var arr = JSON.parse(localStorage.getItem("GWC"));
                                                    var fruitStr = '';
                                                    for (var i = 0; i < arr.length; i++) {
                                                        var obj2 = arr[i];
                                                        if (obj2.fruitObj.shop.id == obj.shopId && obj2.isFujin == false) {
                                                            fruitStr = $.trim(fruitStr + ' ' + obj2.fruitObj.name)
                                                        }
                                                    }
                                                    $('#page_8_1_listview_dd').append('<li><a onclick="go83(' + obj.shopId + ', false)"href="javascript:void(0)"data-transition="slide "style="border-color:#FFFFFF"class="ui-btn ui-btn-icon-right ui-icon-carat-r"><div style="font-size:18px"><p>' + obj.shopName + '</p><p>选择到货的订单</p><p>营业时间：<span>' + obj.openTime + '-' + obj.closeTime + '</span></p><p style="white-space:pre-wrap">' + txt + '</p><p style="white-space:pre-wrap">' + fruitStr + '</p></div></a></li>');
                                                    $('#page_8_1').trigger('create');
                                                    changePage("#page_8_1")
                                                } else {
                                                    debugLog('商店不存在')
                                                }
                                            },
                                            eFunc: function(XMLHttpRequest, textStatus, emessage) {
                                                messageBox("未知错误")
                                            }
                                        })
                                    } else {
                                        $('#page_8_1').trigger('create');
                                        changePage("#page_8_1")
                                    }
                                }
                            } else {
                                $('#page_8_1').trigger('create');
                                changePage("#page_8_1")
                            }
                        } else {
                            messageBox(json.msg)
                        }
                    },
                    eFunc: function(XMLHttpRequest, textStatus, emessage) {
                        messageBox("未知错误")
                    }
                })
            }
        }
        if (!hasFlag) {
            arr3 = new Array();
            if (arr2[1].length > 0) {
                for (var i = 0; i < arr2[1].length; i++) {
                    var o = arr2[1][i];
                    var hasExist = false;
                    for (var j = 0; j < arr3.length; j++) {
                        if (arr3[j] == o.shopObj.shopId) {
                            hasExist = true;
                            break
                        }
                    }
                    if (!hasExist) {
                        arr3.push(o.shopObj.shopId);
                        JKjsonP({
                            ws: "customer/getShopById",
                            parameter: {
                                shopId: o.shopObj.shopId,
                                lat: lat,
                                lng: lng
                            },
                            sFunc: function(json) {
                                if (json.result == "success") {
                                    var obj = json.data;
                                    var distance31 = obj.distance;
                                    var shippingArr = obj.shipping;
                                    var len2 = shippingArr.length;
                                    var distance312 = 0;
                                    var txt = "";
                                    if (distance31 <= 1000) {
                                        distance312 = 1
                                    } else if (distance31 > 1000 || distance31 <= 2000) {
                                        distance312 = 2
                                    } else if (distance31 > 2000 || distance31 <= 3000) {
                                        distance312 = 3
                                    } else if (distance31 > 3000 || distance31 <= 4000) {
                                        distance312 = 4
                                    } else if (distance31 > 4000 || distance31 <= 5000) {
                                        distance312 = 5
                                    }
                                    for (var j = 0; j < len2; j++) {
                                        if (distance312 == shippingArr[j].distance) {
                                            var shippingPrice31 = shippingArr[j].shippingPrice;
                                            var freeShippingPrice31 = shippingArr[j].freeShippingPrice;
                                            var startPrice31 = shippingArr[j].startPrice;
                                            if (shippingPrice31 == "0" && freeShippingPrice31 == "0") {
                                                txt = "满" + startPrice31 + "元起送"
                                            } else if (startPrice31 == "0") {
                                                txt = "满" + freeShippingPrice31 + "免" + shippingPrice31 + "元送货费"
                                            }
                                        }
                                    };
                                    var arr = JSON.parse(localStorage.getItem("GWC"));
                                    var fruitStr = '';
                                    for (var i = 0; i < arr.length; i++) {
                                        var obj2 = arr[i];
                                        if (obj2.fruitObj.shop.id == obj.shopId && obj2.isFujin == false) {
                                            fruitStr = $.trim(fruitStr + ' ' + obj2.fruitObj.name)
                                        }
                                    }
                                    $('#page_8_1_listview_dd').append('<li><a onclick="go83(' + obj.shopId + ', false)"href="javascript:void(0)"data-transition="slide "style="border-color:#FFFFFF"class="ui-btn ui-btn-icon-right ui-icon-carat-r"><div style="font-size:18px"><p>' + obj.shopName + '</p><p>选择到货的订单</p><p>营业时间：<span>' + obj.openTime + '-' + obj.closeTime + '</span></p><p style="white-space:pre-wrap">' + txt + '</p><p style="white-space:pre-wrap">' + fruitStr + '</p></div></a></li>');
                                    $('#page_8_1').trigger('create');
                                    changePage("#page_8_1")
                                } else {
                                    $('#page_8_1').trigger('create');
                                    changePage("#page_8_1")
                                }
                            },
                            eFunc: function(XMLHttpRequest, textStatus, emessage) {
                                $('#page_8_1').trigger('create');
                                changePage("#page_8_1")
                            }
                        })
                    }
                }
            } else {
                $('#page_8_1').trigger('create');
                changePage("#page_8_1")
            }
        }
    } catch (e) {
        debugLog(e);
        messageBox("未知错误");
        localStorage.removeItem("GWC");
    }
}

var changeIndex=null;var changeShopId=null;var changeFlag=null;var changeFruitId=null;var changeFruitObj=null;
function changeItem(index,shopId,flag,fruitId){debugLog(index+""+shopId+""+flag+""+fruitId);changeIndex=index;changeShopId=shopId;changeFlag=flag;changeFruitId=fruitId;var arr=JSON.parse(localStorage.getItem("GWC"));var tmpData=new Array();for(var i=0;i<arr.length;i++){var obj=arr[i];if(obj.fruitObj.id==fruitId&&obj.isFujin==flag&&obj.fruitObj.shop.id==shopId){currentIsFujin=obj.isFujin;currentShopObj=obj.shopObj;changeFruitObj=obj.fruitObj;debugLog(JSON.stringify(obj));$('#page_8_2').remove();$('#page_8_3').append('<div data-role="popup"id="page_8_2"style="padding:10px; height:300px; "data-dismissible="false"data-theme="jk"data-transition="slideup"><span style="font-size:20px; color:#FFFFFF">'+obj.fruitObj.name+'</span><div><div class="ui-grid-a"><div class="ui-block-a"style="text-align:justify;text-justify :distribute-all-lines;"><input type="text"id="how71"onkeyup="this.value=this.value.replace(/\\D/g,\'\');calculate83(\''+obj.util+'\')" onafterpaste="this.value=this.value.replace(/\\D/g,\'\')"onChange="calculate83(\''+obj.util+'\')"value="'+obj.number+'"style="background-color:#FFFFFF"></div><span class="ui-block-b ui-group-theme-b"><select name="liang"id="liang71"style="width:110px"onChange="calculate83(\''+obj.util+'\')"class=" ui-icon-arrow-d ui-btn-icon-left"><option value="'+obj.fruitObj.util+'">'+obj.fruitObj.util+'</option><option value="斤">斤</option><option value="元">元</option></select></span><div class="ui-block-b"style="width:50px"></div></div><span id="calculate71"style="font-size:14px">'+obj.number+obj.fruitObj.util+'，'+(obj.number*obj.fruitObj.weight)+'斤，'+(obj.number*obj.fruitObj.price)+'元</span><label for="ask82"style="font:bolder">要求：</label><input type="text"id="ask71"name="ask82"value="'+obj.comment+'"style="background-color:#FFF"><div class="ui-grid-a"><div class="ui-block-a"><a href="javascript:void(0)" class="ui-btn ui-corner-all"onClick="buy62()"style="background-color:#94252a">确定</a></div><div class="ui-block-b"><a class="ui-btn ui-corner-all"data-rel="back"data-icon="back"style="background-color:#94252a">返回</a></div></div></div></div><div data-role="popup"id="page_7_2"><img src="1112.jpg"style="width:100%"></div>');$('#page_8_3').trigger('create');$('#page_8_2').popup('open');break}}}

function calculate83(ttt){currentFruitObj=changeFruitObj;calculate71(ttt)}

function buy62(){var comment=$('#ask71').val();var number=$.trim($('#how71').val());var type=$('#liang71').val();if(number!=''){var number=$('#how71').val();var type=$('#liang71').val();if($.trim($('#how71').val())!=''){if(type.indexOf("十")>=0){ge=$('#how71').val()*10;jin=ge/currentFruitObj.weight;yuan=jin*currentFruitObj.price}else if(type=='斤'){jin=$('#how71').val();yuan=jin*currentFruitObj.price;ge=jin/currentFruitObj.weight}else if(type=='元'){yuan=$('#how71').val();jin=yuan/currentFruitObj.price;ge=jin/currentFruitObj.weight}else{ge=$('#how71').val();jin=ge/currentFruitObj.weight;yuan=jin*currentFruitObj.price}}ge=Math.round(ge*100)/100;jin=Math.round(jin*100)/100;yuan=Math.round(yuan*100)/100;$('#calculate71').html(ge+wt+'，'+jin+'斤，'+yuan+'元');var arr=JSON.parse(localStorage.getItem("GWC"));var tmpData=new Array();for(var i=0;i<arr.length;i++){var o=arr[i];if(o.fruitObj.id==changeFruitId&&o.isFujin==changeFlag&&o.fruitObj.shop.id==changeShopId){}else{tmpData.push(o)}}localStorage.setItem("GWC",JSON.stringify(tmpData));var arr=localStorage.getItem("GWC");if(arr==null){arr=new Array()}else{arr=JSON.parse(arr)}var obj={fruitObj:changeFruitObj,shopObj:currentShopObj,number:number,comment:$('#ask71').val(),isFujin:currentIsFujin,util:type,totalPrice:yuan};arr[arr.length]=obj;localStorage.setItem("GWC",JSON.stringify(arr));messageBox("操作成功");$('#page_8_2').hide();gotoGwc()}}

function removeItem(index, shopId, flag, fruitId) {
    if (confirm("确认删除？")) {
        var arr = JSON.parse(localStorage.getItem("GWC"));
        var tmpData = new Array();
        for (var i = 0; i < arr.length; i++) {
            var o = arr[i];
            if (o.fruitObj.id == fruitId && o.isFujin == flag && o.fruitObj.shop.id == shopId) {} else {
                tmpData.push(o);
            }
        }
        localStorage.setItem("GWC", JSON.stringify(tmpData));
        $('#page_8_3_item' + index).remove();

        orderObj.fruitTotalPrice -= orderObj.fruits[index].totalPrice;
        orderObj.fruits[index] = null;
        update83();
        count--;
        if (count <= 0) {
            gotoGwc();
        }
    }
}

var qisong;
var count;
/**
 * 点击查看订单详情
 */
function go83(shopId, flag) {
    qisong  = {
        status: 0,
        startPrice: 0,
        freePrice: 0,
        shipPrice: 0
    };

    count = 0;
    orderObj.shopId = shopId;

    currentIsFujin = flag;
    orderObj.isFujin = flag;

    

    var arr = JSON.parse(localStorage.getItem("GWC"));
    if (null == arr) {
        messageBox("购物车无水果");
        changePage("#page_3_1");
        return;
    }

    orderObj.fruitTotalPrice = 0;

    var data = new Array();

    for (var i = 0; i < arr.length; i++) {
        var obj = arr[i];
        if (obj.fruitObj.shop.id == shopId && obj.isFujin == flag) {
            var o = {
                fruitId: obj.fruitObj.id,
                fruitName: obj.fruitObj.name,
                quantity: obj.number,
                util: obj.util,
                totalPrice: obj.totalPrice,
                comment: obj.comment,
                shopId: shopId,
                flag: flag
            };
            orderObj.addressId = obj.addressId;
            orderObj.addressName = obj.addressName;
            data.push(o);
            orderObj.fruitTotalPrice += o.totalPrice;

            orderObj.shipPrice = obj.fruitObj.shop.id.shipping;
        }
    }
    orderObj.fruits = data;


    var obj;
    var txt = "";
    JKjsonP({
        ws: "customer/getShopById",
        parameter: {
            shopId: shopId,
            lat: lat,
            lng: lng
        },
        sFunc: function(json) {
            if (json.result == "success") {
                $('#page_8_3').html('<div data-role="header"style=" border-color:#FFFFFF"><a class="ui-btn ui-corner-all"data-rel="back"data-icon="back"style="border:none"><img src="返回.png"style="height:20px;width:20px"/></a></a><h1>我的购物车</h1><a class="ui-btn ui-corner-all ui-btn-right "onClick="panel()"data-transition="slide"style="border:none;margin-top:-1.5px;"><img src="menu.png"style="height:25px;width:25px"/></a></div>');

                obj = json.data;
                var distance31 = obj.distance;
                var shippingArr = obj.shipping;
                var len2 = shippingArr.length;
                var distance312 = 0;

                if (distance31 <= 1000) {
                    distance312 = 1;
                } else if (distance31 > 1000 || distance31 <= 2000) {
                    distance312 = 2;
                } else if (distance31 > 2000 || distance31 <= 3000) {
                    distance312 = 3;
                } else if (distance31 > 3000 || distance31 <= 4000) {
                    distance312 = 4;
                } else if (distance31 > 4000 || distance31 <= 5000) {
                    distance312 = 5;
                }

                var shippingPrice31;
                var freeShippingPrice31;
                var startPrice31;
                for (var j = 0; j < len2; j++) {
                    if (distance312 == shippingArr[j].distance) {
                        shippingPrice31 = shippingArr[j].shippingPrice;
                        freeShippingPrice31 = shippingArr[j].freeShippingPrice;
                        startPrice31 = shippingArr[j].startPrice;
                        if (shippingPrice31 == "0" && freeShippingPrice31 == "0") {
                            qisong.status = 1;
                            qisong.startPrice = startPrice31;
                            txt = "满" + startPrice31 + "元起送";
                        } else if (startPrice31 == "0") {
                            qisong.status = 2;
                            qisong.freePrice = freeShippingPrice31;
                            qisong.shipPrice = shippingPrice31;
                            txt = "满" + freeShippingPrice31 + "免" + shippingPrice31 + "元送货费";
                        }
                        break;
                    }
                };
                orderObj.shipPrice = shippingPrice31;

                $('#page_8_3').html('<div data-role="content"><div class="ui-grid-a"><div class="ui-block-a"><a class="ui-btn ui-corner-all"style="background-color:#94252a"onClick="go61(' + shopId + ')">添加水果</a></div><div class="ui-block-b"><p align="right"style="color:#fff">' + txt + '</p></div></div><ul data-role="listview"style="margin-top:1px;"id="page_8_3_listview_dd"></ul><p style="font-size:14px;margin-top:20px; color:#FFFFFF;">由于水果的重量难以准确掌控，实际到货价格可能会有上下20%的浮动</p><div data-role="popup"id="page_8_2_1"style="padding:10px; height:300px; "data-dismissible="false"data-theme="jk"data-transition="slideup"><div><span style="font-size:20px; color:#FFFFFF">' + obj.FruitName + '</span><div><div class="ui-grid-a"><div class="ui-block-a"style="text-align:justify;text-justify :distribute-all-lines;"><input type="text"id="how82"onChange="calculate()"style="background-color:#FFFFFF"></div><span class="ui-block-b ui-group-theme-b"><select name="liang"id="liang"class=" ui-icon-arrow-d ui-btn-icon-left"><option value="ge">个</option><option value="jin">斤</option><option value="yuan">元</option></select></span><div class="ui-block-b"style="width:50px"></div></div><span class="calculate82"style="font-size:14px">4个，10斤，40元</span><label for="ask82"style="font:bolder">要求：</label><input type="text"id="ask82"name="ask82"style="background-color:#FFF"><div class="ui-grid-a"><div class="ui-block-a"><a class="ui-btn ui-corner-all"style="background-color:#fff; color:#000000"onClick="buy821()">确定</a></div><div class="ui-block-b"><a class="ui-btn ui-corner-all"data-rel="back"data-icon="back"style="background-color:#fff; color:#000000">返回</a></div></div></div></div></div></div>');

                // 填充UI
                var length = data.length;
                for (var i = 0; i < length; i++) {
                    count++;
                    var obj = data[i];
                    $('#page_8_3_listview_dd').append('<li id="page_8_3_item' + i + '"style="background-color:#f18a0c;  border-color:#FFFFFF"><div><p style="font-size:14px;">' + obj.fruitName + '</p><p style="font-size:10px;">' + obj.quantity + obj.util + '，' + obj.totalPrice + '元</p><p style="font-size:10px;"><span>要求：</span><span style="white-space:pre-wrap">' + obj.comment + '</span></p><div class="ui-grid-a"><div class="ui-block-a"><a onclick="changeItem(' + i + ',' + obj.shopId + ',' + obj.flag + ',' + obj.fruitId + ')"class="ui-btn ui-corner-all ui-mini ui-btn-a"href="javascript:void(0);"style="background-color:#94252a;font-size:14px;color:#fff;">修改</a></div><div class="ui-block-b"><a onclick="removeItem(' + i + ',' + obj.shopId + ',' + obj.flag + ',' + obj.fruitId + ')"class="ui-btn ui-corner-all ui-mini"style="background-color:#94252a;font-size:14px;">删除</a></div></div></div></li>');
                }

                $('#page_8_3_jiesuan').remove();
                $('#page_8_3_listview_dd').parent().append('<div id="page_8_3_jiesuan" class="ui-grid-a"style="margin-top:-30px;"><div class="ui-block-a" style="width:70%"><p style="margin-top:30px; color:#FFFFFF; font-size:14px">' + txt + '</p><p style="margin-top:-15px; color:#FFFFFF; font-size:14px">送货费' + shippingPrice31 + '元</p><p style="margin-top:-15px; color:#FFFFFF; font-size:14px">水果价格<span id="page_8_3_sgjg">' + orderObj.fruitTotalPrice + '</span>元</p><p style="margin-top:-15px"><div class="ui-checkbox"><label for="no83" style="border:none; color:#aa0001; font-size:10px;" class="ui-btn ui-corner-all ui-btn-inherit ui-btn-icon-left ui-checkbox-off"><a style="margin-left:10px; color:#fff;">实际价格不超过合计价格</a></label><input type="checkbox"id="no83"></div></p></div><div class="ui-block-b" style="width:30%"><p style="margin-top:50px; align="right">合计<span id="page_8_3_hj">' + (orderObj.fruitTotalPrice + shippingPrice31) + '</span>元</p></div></div><p align="center"><a onclick="go91()" class="ui-btn ui-corner-all" style="background-color:#94252a;  width:150px"href="javascript:void(0)"data-transition="slide">结算</a></P>');

                $('#page_8_3').trigger('create');
                $('#page_8_3').page();
                changePage("#page_8_3");
            } else {
                messageBox(json.msg);
            }
        },
        eFunc: function(XMLHttpRequest, textStatus, emessage) {
            messageBox('服务器错误');
        }
    });

}
function go91() {
    if (qisong.status == 1 && orderObj.fruitTotalPrice < qisong.startPrice) {
        messageBox("未达到起送价");
        return;
    }
    orderObj.limitFlag = $('#no83').attr('data-cacheval') == "false" ? true: false;
    if (orderObj.isFujin) {
        listAddress()
    } else {
        go131(orderObj.addressId, orderObj.addressName)
    }
}
function update83() {
    $('#page_8_3_sgjg').html(orderObj.fruitTotalPrice);

    if (qisong.status == 2) {
        var shipPrice = orderObj.shipPrice;
        if (orderObj.fruitTotalPrice >= qisong.freePrice) {
            shipPrice = shipPrice >= qisong.shipPrice ? shipPrice - qisong.shipPrice : 0;
            orderObj.shipPrice = shipPrice;
        }
    }

    $('#page_8_3_hj').html(orderObj.fruitTotalPrice + shipPrice);
}

function go131(addressId, inAddressName) {

    addressId = addressId;
    addressName = inAddressName;

    orderObj.addressId = addressId;
    orderObj.addressName = inAddressName;

    var hour = new Date().getHours();
	var minutes = new Date().getMinutes();
	
	if(minutes>=20){
		$('#good114').val(hour+1 > 24 ? 24 : hour+1);
        $('#fast114').val(hour + 2 > 24 ? 24 : hour + 2);
	}else{
        $('#good114').val(hour);
        $('#fast114').val(hour + 1 > 24 ? 24 : hour + 1);
	}
    JKjsonP({
        ws: "merchants/listTodayOrder",
        parameter: {
            shopId: orderObj.shopId
        },
        sFunc: function(json) {
            if ("success" == json.result) {
                var orders = json.data;
                listTodayOrder = json.data;

                JKjsonP({
                    ws: "merchants/listVehicleCount",
                    parameter: {
                        shopId: orderObj.shopId
                    },
                    sFunc: function(json) {
                        if ("success" == json.result) {
                            JKjsonP({
                                ws: "merchants/listDeliveryMan",
                                parameter: {
                                    shopId: orderObj.shopId
                                },
                                sFunc: function(json2) {
                                    debugLog("获取结果：" + JSON.stringify(json2));
                                    if ("success" == json2.result) {
                                        
                                        var deliveryManNum = json2.data;

                                        if (deliveryManNum.length >= 1) {
                                            var data = json.data;

                                            var params = {
                                                deliverymans: {
                                                    bike: data["1"],
                                                    electricMobile: data["2"],
                                                    car: data["3"]
                                                }
                                            };
                                            deliverOrder = new DeliveryOrder(params);
                                            

                                            var tmpOrders = [];
                                            for (var i = 0; i < orders.length; i++) {
                                                var o = orders[i];
                                                var index = parseInt(o.shipStartTime.substr(0, o.shipStartTime.indexOf(":")));
                                                tmpOrders[index] = tmpOrders[index] == null ? new Array() : tmpOrders[index];
                                                tmpOrders[index].push(o)
                                            }

                                            debugLog("时间段已满");
                                            debugLog(JSON.stringify(params));
                                            debugLog(JSON.stringify(tmpOrders));
                                            $('#noTime').html(deliverOrder.getStr(tmpOrders));
                                            changePage('#page_13_1');
                                        } else {
                                            messageBox("送货人数不足");
                                        }
                                    } else {
                                        messageBox("获取送货员数量失败");
                                    }
                                },
                                eFunc: function(XMLHttpRequest, textStatus, emessage) {
                                    messageBox("服务器返回错误");
                                }
                            });
                        } else {
                            messageBox("获取交通工具数量失败");
                        }
                    },
                    eFunc: function(XMLHttpRequest, textStatus, emessage) {
                        messageBox("服务器解析数据错误");
                    }
                });
            }
        },
        eFunc: function(XMLHttpRequest, textStatus, emessage) {
            messageBox("服务器解析数据错误");
        }
    });
}

function go101() {
	flagSaving = false;
    
    orderObj.fast = $('#fast131').attr('data-cacheval') == "false" ? true: false;

    orderObj.arriveBeginClock = $('#good114').val() + ":00";
    orderObj.shipStartTime = orderObj.arriveBeginClock;
    orderObj.arriveEndClock = $('#fast114').val() + ":00";
    orderObj.shipEndTime = orderObj.arriveEndClock;
    var currentTime = new Date();
    currentTime = currentTime.getHours().toString() + ':' + currentTime.getMinutes().toString();
    orderObj.orderClock = currentTime;

    JKjsonP({
        ws: "merchants/listTodayOrder",
        parameter: {
            shopId: orderObj.shopId
        },
        sFunc: function(json) {
            if ("success" == json.result) {
                var orders = json.data;

                debugLog(currentTime);
                debugLog(JSON.stringify(orderObj));
                debugLog(JSON.stringify(orders));
                if (deliverOrder.checkDeliveryTimeCanService(currentTime, orderObj, orders)) {
                    JKjsonP({
                        ws: "customer/getShopById",
                        parameter: {
                            shopId: orderObj.shopId,
                            lat: window.lat,
                            lng: window.lng
                        },
                        sFunc: function(json) {
                            if (json.result == "success") {
                                var obj = json.data;

                                var distance31 = obj.distance;
                                var shippingArr = obj.shipping;
                                var len2 = shippingArr.length;
                                var distance312 = 0;
                                var txt = "";

                                if (distance31 <= 1000) {
                                    distance312 = 1;
                                } else if (distance31 > 1000 || distance31 <= 2000) {
                                    distance312 = 2;
                                } else if (distance31 > 2000 || distance31 <= 3000) {
                                    distance312 = 3;
                                } else if (distance31 > 3000 || distance31 <= 4000) {
                                    distance312 = 4;
                                } else if (distance31 > 4000 || distance31 <= 5000) {
                                    distance312 = 5;
                                }

                                for (var j = 0; j < len2; j++) {
                                    if (distance312 == shippingArr[j].distance) {
                                        var shippingPrice31 = shippingArr[j].shippingPrice;
                                        var freeShippingPrice31 = shippingArr[j].freeShippingPrice;
                                        var startPrice31 = shippingArr[j].startPrice;
                                        if (shippingPrice31 == "0" && freeShippingPrice31 == "0") {
                                            txt = "满" + startPrice31 + "元起送";
                                        } else if (startPrice31 == "0") {
                                            txt = "满" + freeShippingPrice31 + "免" + shippingPrice31 + "元送货费";
                                        }
                                    }
                                };

                                var arriveTime31 = obj.arriveTime;
                                orderObj.orderNo = localStorage.getItem("customerId") + (new Date().getTime());
                                $('#page_10_1_listview li').remove();
                                $('#page_10_1_listview').append('<li data-role="list-divider"role="heading"class="ui-li-divider ui-bar-inherit ui-first-child"><div class="ui-grid-a"><div class="ui-block-a"><p>' + obj.shopName + '</p><p>最快' + deliverOrder.getArriveTime(orders) + '</p><p>距离' + obj.distance + '公里</p></div><div class="ui-block-b"><p>订单号：' + orderObj.orderNo + '</p><p>营业：' + obj.openTime + '到' + obj.closeTime + '</p><p>' + txt + '</p></div></div></li>');

                                for (var i = 0; i < orderObj.fruits.length; i++) {
                                    var obj = orderObj.fruits[i];
                                    if (null != obj) {
                                        $('#page_10_1_listview').append('<li style="border-color:#FFFFFF; background-color:#f18a0c;color:#FFF"class="ui-li-static ui-body-inherit"><p style="font-size:20px">' + obj.fruitName + '</p><span style="font-size:16px">' + obj.quantity + obj.util + obj.totalPrice + '元</span><span style="margin-left:20px;font-size:12px">' + obj.comment + '</span></li>');
                                    }
                                }

                                $('#page_10_1').page();
                                $('#page_10_1_listview_bottom').remove();
                                $('#page_10_1_listview').parent().append('<div id="page_10_1_listview_bottom"class="ui-grid-a"style="margin-top:20px;font-size:14px"><div class="ui-block-a"style="width:60%"><p>水果总价：' + orderObj.fruitTotalPrice + '元&nbsp;运费：' + orderObj.shipPrice + '元</p><p>合计：' + (orderObj.fruitTotalPrice + orderObj.shipPrice) + '元</p><p>' + orderObj.shipStartTime + '点-' + orderObj.shipEndTime + '点送达</p>' + (orderObj.limitFlag == true ? '<p>实际价格不超过总价</p>': '') + '</div><div class="ui-block-b"style="width:30%"><p><a href="javascript:void(0);" onClick="changePage(\'#page_8_3\')" class="ui-mini ui-btn ui-shadow ui-corner-all ">修改订单</a></p><p><a class=" ui-mini ui-btn ui-shadow ui-corner-all "href="javascript:void(0)"data-rel="popup"data-position-to="window"data-transition="pop" onClick="changePage(\'#page_13_1\')">修改时间</a></p></div><div style="clear:both"></div><div class="ui-grid-a"style="margin-top:20px;font-size:14px"><div class="ui-block-a"style="width:30%;margin-top:8px"><p>收货地址：</p></div><div class="ui-block-b"style="width:60%"><p><a href="javascript:void(0)" onClick="changePage(\'#page_9_1\')" class="ui-mini ui-btn ui-shadow ui-corner-all">修改收货地址</a></p></div></div><span style="font-size:14px">' + orderObj.addressName + '</span></div>').trigger("create");
                                $('#page_10_1_listview').listview("refresh");

                                changePage("#page_10_1");
                            } else {
                                messageBox(json.msg);
                            }
                        },
                        eFunc: function(XMLHttpRequest, textStatus, emessage) {
                            messageBox("服务器返回错误");
                            changePage("page_3_1");
                        }
                    });

                } else {
                    messageBox("此订单不能在此时间段内派送");
                }
            } else {
                messageBox("失败");
            }
        },
        eFunc: function(XMLHttpRequest, textStatus, emessage) {
            messageBox("服务器返回错误");
            changePage("page_3_1");
        }
    });
}

var flagSaving = false;
/**
 * 提交订单
 */
function JKClickSaveOrder() {
    if (flagSaving) {
        return false;
    }
	flagSaving = true;
    $.mobile.loading("show", {
        text: '请等待...',
        textVisible: true,
        theme: "b",
        textonly: false,
        html: ""
    });
    
    var fruits = new Array();
    var length = orderObj.fruits.length;
    for (var i = 0; i < length; i++) {
        var obj = orderObj.fruits[i];
        if (null != obj) {
            fruits.push({
                fruitId: obj.fruitId,
                quantity: obj.quantity,
                util: obj.util,
                totalPrice: obj.totalPrice,
                comment: obj.comment
            })
        }
    }
    var str = JSON.stringify(fruits);
    listTodayOrder.push(orderObj);
    for (var i = 0; i < listTodayOrder.length; i++) {
        listTodayOrder[i].distance = getDistance(listTodayOrder[i].lat, listTodayOrder[i].lng, window.lat, window.lng);
        listTodayOrder[i].id = listTodayOrder[i].orderId
    }
    debugLog(listTodayOrder);
    debugLog("开始calculateDeliveryOrder");
    var newOrders = deliverOrder.calculateDeliveryOrder(parseInt(orderObj.orderClock.substr(0, orderObj.orderClock.indexOf(":"))), listTodayOrder);
    debugLog(newOrders);
    if (newOrders.length <= 0) {
        messageBox("分配失败");
        debugLog("分配失败");
        changePage("#page_3_1");
        $.mobile.loading("hide");
        return;
    }
    debugLog("分配成功");
    debugLog(JSON.stringify(newOrders));
    // var flag = false;

    // for (var i = 0; i <= 26; i++) {
    //     if (newOrders[i] != null) {
    //         debugLog(i);
    //         debugLog(newOrders[i]);
    //         for (var j = 0; j < newOrders[i].length; j++) {
    //             alert(newOrders[i][j].id + ' ' + newOrders[i][j].orderId);
    //             if (newOrders[i][j].id == null && newOrders[i][j].orderId == null) {
    //                 alert(i);
    //                 orderObj.shipStartTime = i + ":00";
    //                 orderObj.shipEndTime   = (i + 1) + ":00";
    //                 flag = true;
    //                 break;
    //             }
    //         }
    //     }
    // }
    
    // if (!flag) {
    //     messageBox("分配失败");
    //     return;
    // }

    JKjsonP({
        ws: "merchants/listDeliveryMan",
        parameter: {
            shopId: orderObj.shopId
        },
        sFunc: function(json) {
            if ("success" == json.result) {
                if (json.data.length >= 1) {
                    var deliveryMan = [];
                    var deliveryManNum = json.data;
                    debugLog(JSON.stringify(newOrders));
                    debugLog("提交当前表单");
                    var senddd = {
                        orderNo: localStorage.getItem("customerId") + (new Date().getTime()),
                        customerId: localStorage.getItem("customerId"),
                        shopId: orderObj.shopId,
                        addressId: orderObj.addressId,
                        fruitTotalPrice: orderObj.fruitTotalPrice,
                        shipPrice: orderObj.shipPrice,
                        shipStartTime: orderObj.shipStartTime,
                        shipEndTime: orderObj.shipEndTime,
                        limitFlag: orderObj.limitFlag,
                        fruits: str
                    };
                    debugLog("提交数据：" + JSON.stringify(senddd));

                    JKjsonP({
                        ws: "customer/saveOrder",
                        parameter: {
                            orderNo: orderObj.orderNo,
                            customerId: localStorage.getItem("customerId"),
                            shopId: orderObj.shopId,
                            addressId: orderObj.addressId,
                            fruitTotalPrice: orderObj.fruitTotalPrice,
                            shipPrice: orderObj.shipPrice,
                            shipStartTime: orderObj.shipStartTime,
                            shipEndTime: orderObj.shipEndTime,
                            limitFlag: orderObj.limitFlag,
                            fruits: str
                        },
                        sFunc: function(json) {
                            debugLog("提交结果：" + JSON.stringify(json));
                            if ("success" == json.result) {

                                var arr=JSON.parse(localStorage.getItem("GWC"));var tmpData=new Array();for(var i=0;i<arr.length;i++){var o=arr[i];var hasExist=false;for(var j=0;j<fruits.length;j++){var oo=fruits[j];if(o.fruitObj.id==oo.fruitId&&o.isFujin==currentIsFujin&&o.fruitObj.shop.id==orderObj.shopId){hasExist=true;break}}if(!hasExist){tmpData.push(o)}}localStorage.setItem("GWC",JSON.stringify(tmpData));

                                JKjsonP({
                                    ws: "merchants/listTodayOrder",
                                    parameter: {
                                        shopId: orderObj.shopId
                                    },
                                    sFunc: function(json) {
                                        if ("success" == json.result) {
                                            var orders = json.data;
                                            listTodayOrder = json.data;

                                            var newOrders = deliverOrder.calculateDeliveryOrder(parseInt(orderObj.orderClock.substr(0, orderObj.orderClock.indexOf(":"))), listTodayOrder);
                                            if (newOrders.length <= 0) {
                                                messageBox("分配失败");
                                                debugLog("分配失败");
                                                changePage("#page_3_1");
                                                $.mobile.loading("hide");
                                                return;
                                            }
                                            debugLog("分配成功");
                                            debugLog(newOrders);
                                            var aaab = ["8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"];
                                            var ttt = new Array();
                                            for (var i = 0; i < 28; i++) {
                                                var arr = newOrders[i];
                                                if (arr != null) {
                                                    for (var j = 0; j < arr.length; j++) {
                                                        var o = {
                                                            orderId: arr[j].orderId,
                                                            deliveryTime: parseInt(_getStdTime2(arr[j].shipStartTime) / 60)
                                                        }
                                                        ttt.push(o)
                                                    }
                                                }
                                            }
                                            debugLog("订单按时间分组");
                                            debugLog(JSON.stringify(ttt));

        
                                            if (deliveryManNum.length > 1) {
                                                var sum = 0;
                                                for (var i = 0; i < deliveryManNum.length; i++) {
                                                    deliveryMan.push(deliveryManNum[i]);
                                                    sum += deliveryManNum[i];
                                                }
                                                
                                                debugLog("分配算法调用");

                                                var oorr = listTodayOrder;
                                                // oorr.sort(function(a, b) {
                                                //     var result = a.distance - b.distance;
                                                //     if (result != 0) {
                                                //         return result;
                                                //     }
                                                //     return a.orderId - b.orderId;
                                                // });
                                                debugLog(oorr.length);
                                                var sss = [];
                                                for (var i = 0; i < oorr.length && i < sum; i++) {
                                                    oorr[i].id = oorr[i].orderId;
                                                    sss.push(oorr[i]);
                                                    debugLog(i);
                                                }
                                                debugLog(sss.length);

                                                var sendData = new Array();
                                                if (sss.length == 0) {
                                                    messageBox("没有订单");
                                                    changePage("#page_3_1");
                                                    $.mobile.loading("hide");
                                                } else{
                                                    var ddd = [];
                                                    if (sss.length == 1) {
                                                        var timerss = new Array();
                                                        var t22 = new Array();
                                                        var tttt = new Array();
                                                        for (var i = 0; i < sss.length; i++) {
                                                            tttt.push({orderId:sss[i].orderId});
                                                        }
                                                        ddd = new Array();
                                                        ddd.push(tttt);
                                                    } else {
                                                        ddd = OrderDeliveryC(sss, sortOrders(sss), deliveryMan, sum);
                                                        debugLog("调用结果：" + JSON.stringify(ddd));
                                                        if (null == ddd) {
                                                            messageBox("订单已提交但分配送货员失败");
                                                            changePage("#page_3_1");
                                                            $.mobile.loading("hide");
                                                            return;
                                                        }
                                                    }
                                                    
                                                    for (var i = 0; i < ddd.length; i++) {
                                                        var aaa = ddd[i];

                                                        var index = i + 1;

                                                        var timers = new Array();
                                                        for (var j = 0; j < aaa.length; j++) {
                                                            var oj = aaa[j];

                                                            for (var k = 0; k < ttt.length; k++) {
                                                                var ok = ttt[k];
                                                                if (ok.orderId == oj.orderId) {
                                                                    if (null == timers[ok.deliveryTime]) {
                                                                        timers[ok.deliveryTime] = new Array();
                                                                    }
                                                                    timers[ok.deliveryTime].push(oj.orderId);
                                                                    break;
                                                                }
                                                            }
                                                        }
                                                        var timerss = new Array();
                                                        for (var ww = 0; ww < 24; ww++) {
                                                            if (timers[ww] != null) {
                                                                var wwo = {
                                                                    time: ww + 1,
                                                                    orders: timers[ww]
                                                                };
                                                                timerss.push(wwo);
                                                            }
                                                        }
                                                        var doo = {
                                                            id: index,
                                                            times: timerss
                                                        };
                                                        sendData.push(doo);
                                                    }
                                                    

                                                    if (sendData.length <= 0) {
                                                        messageBox("分配失败");
                                                        changePage("#page_3_1");
                                                        $.mobile.loading("hide");
                                                        return;
                                                    }
                                                    sendData = JSON.stringify(sendData);
                                                    debugLog("开始提交分配列表2");
                                                    debugLog(sendData);

                                                    JKjsonP({
                                                        ws: "merchants/distributeOrder",
                                                        parameter: {
                                                            orders: sendData,
                                                            shopId: orderObj.shopId
                                                        },
                                                        sFunc: function(json) {
                                                            if ("success" == json.result) {
                                                                $("#page_10_2").popup("open");
                                                                $.mobile.loading("hide");
                                                            } else {
                                                                messageBox("分配失败");
                                                                changePage("#page_3_1");
                                                                $.mobile.loading("hide");
                                                            }
                                                        },
                                                        eFunc: function(XMLHttpRequest, textStatus, emessage) {
                                                            messageBox("服务器返回错误");
                                                            changePage("#page_3_1");
                                                            $.mobile.loading("hide");
                                                        }
                                                    });
                                                    
                                                }
                                            } else if (1 == deliveryManNum.length) {
                                                var sss = [];
                                                var oorr = listTodayOrder;
                                                // oorr.sort(function(a, b) {
                                                //     var result = a.distance - b.distance;
                                                //     if (result != 0) {
                                                //         return result;
                                                //     }
                                                //     return a.orderId - b.orderId;
                                                // });
                                                debugLog("sss1");
                                                debugLog(oorr);
                                                debugLog(sum);
                                                for (var i = 0; i < oorr.length; i++) {
                                                    oorr[i].id = oorr[i].orderId;
                                                    sss.push(oorr[i]);
                                                    debugLog(i);
                                                }
                                                debugLog("sss");
                                                debugLog(sss);
                                                debugLog(sss.length);

                                                var sendData = [];
                                                var ddd = [];
                                                var timerss = new Array();
                                                var t22 = new Array();
                                                var tttt = new Array();
                                                for (var i = 0; i < sss.length; i++) {
                                                    tttt.push({orderId:sss[i].orderId});
                                                }
                                                ddd = new Array();
                                                ddd.push(tttt);
                                                debugLog(ddd);
                                                for (var i = 0; i < ddd.length; i++) {
                                                    var aaa = ddd[i];
                                                    var index = i + 1;

                                                    var timers = new Array();
                                                    for (var j = 0; j < aaa.length; j++) {
                                                        var oj = aaa[j];
                                                        for (var k = 0; k < ttt.length; k++) {
                                                            var ok = ttt[k];
                                                            if (ok.orderId == oj.orderId) {
                                                                if (null == timers[ok.deliveryTime]) {
                                                                    timers[ok.deliveryTime] = new Array();
                                                                }
                                                                timers[ok.deliveryTime].push(oj.orderId);
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    debugLog(timers);
                                                    var timerss = new Array();
                                                    for (var ww = 0; ww < 30; ww++) {
                                                        if (timers[ww] != null) {
                                                            debugLog(ww);
                                                            var wwo = {
                                                                time: ww,
                                                                orders: timers[ww]
                                                            };
                                                            timerss.push(wwo);
                                                        }
                                                    }
                                                    debugLog("timers");
                                                    debugLog(timerss);
                                                    var doo = {
                                                        id: index,
                                                        times: timerss
                                                    };
                                                    sendData.push(doo);
                                                }
                                                
                                                if (sendData.length <= 0) {
                                                    messageBox("分配失败");
                                                    changePage("#page_3_1");
                                                    $.mobile.loading("hide");
                                                    return;
                                                }
                                                sendData = JSON.stringify(sendData);
                                                debugLog("开始提交分配列表3");
                                                debugLog(sendData);

                                                JKjsonP({
                                                    ws: "merchants/distributeOrder",
                                                    parameter: {
                                                        orders: sendData,
                                                        shopId: orderObj.shopId
                                                    },
                                                    sFunc: function(json) {
                                                        if ("success" == json.result) {
                                                            $("#page_10_2").popup("open");
                                                            $.mobile.loading("hide");
                                                        } else {
                                                            messageBox("分配失败");
                                                            changePage("#page_3_1");
                                                            $.mobile.loading("hide");
                                                        }
                                                    },
                                                    eFunc: function(XMLHttpRequest, textStatus, emessage) {
                                                        messageBox("服务器返回错误");
                                                        changePage("#page_3_1");
                                                        $.mobile.loading("hide");
                                                    },
                                                    complete: function() {
                                                        
                                                    }
                                                });
                                            } else {
                                                messageBox("送货人数不足");
                                                changePage("#page_3_1");
                                                $.mobile.loading("hide");
                                                
                                            }
                                                    

                                        } else {
                                            messageBox("失败");
                                            changePage("#page_3_1");
                                            $.mobile.loading("hide");
                                            
                                        }
                                    },
                                    eFunc: function(XMLHttpRequest, textStatus, emessage) {
                                        messageBox("服务器解析数据错误");
                                        changePage("#page_3_1");
                                        $.mobile.loading("hide");
                                    }
                                });
                            } else {
                                messageBox("提交订单失败");
                                changePage("#page_3_1");
                                $.mobile.loading("hide");
                            }
                        },
                        eFunc: function(XMLHttpRequest, textStatus, emessage) {
                            messageBox("服务器返回错误");
                            changePage("#page_3_1");
                            $.mobile.loading("hide");
                            
                        }
                    });
                } else {
                    messageBox("送货人数不足");
                    changePage("#page_3_1");
                    $.mobile.loading("hide");
                }
            } else {
                messageBox("失败");
                changePage("#page_3_1");
                $.mobile.loading("hide");
            }
        },
        eFunc: function(XMLHttpRequest, textStatus, emessage) {
            messageBox("服务器返回错误");
            changePage("#page_3_1");
            $.mobile.loading("hide");
        }
    });

}

/********************订单历史记录****************************/
function go111() {
    if (!checkLogin()) return;;
    $('#page_11_1_list_container').html('');
    var dateList = new Array();
    JKjsonP({
        ws: "customer/listOrderDate",
        parameter: {
            customerId: localStorage.getItem("customerId")
        },
        sFunc: function(json) {
            if (json.result == "success") {
                var data = json.data;
                var length = data.length;

                for (var i = 0; i < length; i++) {
                    var str = data[i];
                    var listViewId = 'page_11_1_listview_' + i;
                    $('#page_11_1_list_container').append('<div data-role="collapsible"class="ui-collapsible ui-collapsible-themed-content"><h1 class="ui-collapsible-heading">' + str + '</h1><ul data-role="listview"style="background-color:#f18a0c"class="ui-listview"id="' + listViewId + '"></ul></div>').trigger("create");
                    goto111Sub(str, listViewId);
                }
            }
        },
        eFunc: function(XMLHttpRequest, textStatus, emessage) {
            messageBox("服务器返回错误");
            changePage("page_3_1");
        }
    });
}
function goto111Sub(str,listViewId){var dateList=new Array();JKjsonP({ws:"customer/listOrderByDate",parameter:{customerId:localStorage.getItem("customerId"),date:str},sFunc:function(json){if(json.result=="success"){var statusStr=new Array("未知状态","未处理","已配货","送货中","已到货","拒绝发货","拒收","已撤单");var data=json.data;var length=data.length;for(var i=0;i<length;i++){var obj=data[i];var fruitsStr="";for(var j=0;j<obj.fruits.length;j++){fruitsStr+=obj.fruits[j]+" "}$('#'+listViewId+'').append('<li class="ui-first-child ui-last-child"><a onclick="go112(this, '+obj.orderId+')"class="ui-btn ui-btn-icon-right ui-icon-carat-r"><div class="ui-grid-a"><div class="ui-block-a"><p style="font-size:18px">'+obj.shopName+'</p></div><div class="ui-block-b"align="right"><p style="font-size:18px">'+statusStr[obj.status]+'</p></div></div><div class="ui-grid-a"><div class="ui-block-a"><p>'+obj.shipStartTime+'点到'+obj.shipEndTime+'点送达</p></div><div class="ui-block-b"><p>价格'+obj.totalPrice+'元</p></div></div><p style="white-space:pre-wrap">'+fruitsStr+'</p></a></li>')}}changePage('#page_11_1')},eFunc:function(XMLHttpRequest,textStatus,emessage){messageBox("服务器返回错误");changePage("page_3_1")}})}

var tmpFid = null;
var tmpOid = null
function go114(obj, id, id2) {
    try {
        tmpFid = id;
        tmpOid = id2;
        if (tmpFid > 0 && tmpOid > 0) {
            $(obj).remove();
            $('#page_11_4').popup('open');
        } else {
            messageBox("未知错误");
        }

    } catch (e) {
        messageBox("未知错误");
    }
}
function pinglun(ooo) {
    var id = tmpFid;
    var oid = tmpOid;
    JKjsonP({
        ws: "customer/fruitMarking",
        parameter: {
            grade: $('#good1141').val(),
            comment: $('#comment1141').val(),
            orderId: oid,
            fruitId: id
        },
        sFunc: function(json) {
            if (json.result == "success") {
                messageBox("提交成功");
                $('#page_11_4').popup('close');
                changePage('#page_11_2');
            } else {
                messageBox(json.msg);
            }
        },
        eFunc: function(XMLHttpRequest, textStatus, emessage) {
            messageBox("服务器返回错误");
            changePage("page_3_1");
        }
    });
}
function go115(obj, id) {
    try {
        tmpOid = id;
        if (tmpOid > 0) {
            $(obj).remove();
            $('#page_11_5').popup('open');
        } else {
            messageBox("未知错误");
        }

    } catch (e) {
        messageBox("未知错误");
    }
}
function pinglun2(ooo) {
    var oid = tmpOid;
    JKjsonP({
        ws: "customer/shopMarking",
        parameter: {
            grade: $('#fast1141').val(),
            orderId: oid
        },
        sFunc: function(json) {
            if (json.result == "success") {
                messageBox("提交成功");
                $('#page_11_5').popup('close');
                changePage('#page_11_2');
            } else {
                messageBox(json.msg);
            }
        },
        eFunc: function(XMLHttpRequest, textStatus, emessage) {
            messageBox("服务器返回错误");
            changePage("page_3_1");
        }
    });
}
function go112(obj, orderId) {
    var txt = $(obj).html();
    $("#page_11_2_content").html(txt + '<hr style="border-color:#FFFFFF">');

    JKjsonP({
        ws: "customer/getOrderDetail",
        parameter: {
            orderId: orderId
        },
        sFunc: function(json) {
            if (json.result == "success") {
                var data = json.data;
                $("#page_11_2_content").append('<p>订单号：' + data.orderNo + '</p><p style="margin-top:-15px">商家手机号：' + data.mobilePhone + '</p><p style="margin-top:-15px">送货地址：' + data.address + '</p><p style="margin-top:-15px">商家地址：' + data.shopAddress + '</p><p style="margin-top:-15px">商家名称：' + data.shopName + '</p><p style="margin-top:-15px">水果品种：</p>');

                var fruits = json.data.fruits;
                for (var i = 0; i < fruits.length; i++) {
                    var obj = fruits[i];
                    $("#page_11_2_content").append('<p style="margin-top:-15px"><a class="ui-link">' + obj.fruitName + '</a>' + obj.quantity + obj.util + '，价格' + obj.totalPrice + '元' + (obj.isMarking != true && txt.indexOf('已到货') > 0 ? '<a style="margin-left:30px;color:#94252a"href="javascript:void(0)"onclick="go114(this,'+obj.fruitId+','+orderId+')"data-rel="popup"data-position-to="window"data-transition="pop"aria-haspopup="true"aria-owns="page_11_4"aria-expanded="false"class="ui-link">评价</a>': '') + '</p><p style="margin-top:-15px">要求：' + obj.comment + '</p>');
                }
                $("#page_11_2_content").append('<p style="margin-top:-15px">水果合计' + data.fruitTotalPrice + '元，送货费' + data.shipPrice + '元，总计' + data.totalPrice + '元</p><p style="margin-top:-15px">下单时间：' + data.dateCreated + '</p><p style="margin-top:-15px">预计到货时间：' + data.shipStartTime + '点到' + data.shipEndTime + '点</p>');
                if (txt.indexOf('已到货') > 0 && json.data.isMarking != true) {
                    $('#page_11_2_content').append('<a style="margin-left:30px;color:#94252a" href="javascript:void(0)" onClick="go115(this,'+orderId+')" data-rel="popup" data-position-to="window" data-transition="pop">评价送货速度 </a>');
                }

                //$("#page_10_2").popup("open");
                $('#page_11_2').page();

                changePage('#page_11_2');
                $.mobile.changePage("#page_11_2", {
                    transition: "slide"
                });
            } else {
                messageBox(json.msg);
            }
        },
        eFunc: function(XMLHttpRequest, textStatus, emessage) {
            messageBox("服务器返回错误");
            changePage("page_3_1");
        }
    });

}

// function addAddress() {
//     var name92 = $('#name92').val();
//     var add92 = $('#add92').val();

//     if (name92 == '' || add92 == '') {
//         messageBox('请填写完整');
//         return false;
//     }

//     var tmpLat = lat > 0 ? lat: 100;
//     var tmpLng = lng > 0 ? lng: 100;

//     JKjsonP({
//         ws: "customer/addAddress",
//         parameter: {
//             customerId: localStorage.getItem("customerId"),
//             lat: tmpLat,
//             lng: tmpLng,
//             address: (name92 + ' ' + add92)
//         },
//         sFunc: function(json) {
//             messageBox(json.msg);
//             if (json.result == "success") {
//                 $('#page_9_2').popup("close");
//             } else {
//                 messageBox("添加失败");
//             }
//         },
//         eFunc: function(XMLHttpRequest, textStatus, emessage) {
//             messageBox("服务器返回错误");
//             changePage("page_3_1");
//         }
//     });

//     // 解析地址的经纬度
//     var script = document.createElement('script');
//     script.type = 'text/javascript';
//     var newURL = "http://api.map.baidu.com/geocoder/v2/?ak=buGNqMtjcRIStgE0EzDy4pEH&callback=addAddressSub&output=json&address=" + add92 + "&city=" + name92;
//     script.src = newURL;
//     document.body.appendChild(script);

// }

function addAddressSub(json){if(DEBUG){messageBox(json.status)}messageBox('这里是百度地图提交');if(json.status==0){var location=json.location;var tmpLat=location.lat;var tmpLng=location.lng;if(tmpLat-lat>20||tmpLng-lng>20){messageBox('位置匹配错误');return false}}return true}